TWB.New_Module({
	///////////////////////////
	name : "Units",          //
	author : "enduo",        //
	version : 1.0,           //
	///////////////////////////
	
	Action : null,
	Show : function(targ){
		var i;
		targ=targ || window;
		var target=(typeof targ=="undefined")?document:targ.document;
		var btns=target.getElementsByClassName("twb-units");
		for(i=0; i<btns.length; i++){
			btns[i].setAttribute("hidden",false);
		}
	},
	Hide : function(targ){
		var i;
		targ=targ || window;
		var target=(typeof targ=="undefined")?document:targ.document;
		var btns=target.getElementsByClassName("twb-units");
		for(i=0; i<btns.length; i++){
			btns[i].setAttribute("hidden",true);
		}
	},
	Delete : function(e){
		e=e.target;
		e.previousSibling.parentNode.removeChild(e.previousSibling);
		e.parentNode.removeChild(e);
	},
	Spear : function(targ,cnt){
		targ=targ || window;
		var target=(typeof targ=="undefined")?document:targ.document;
		cnt=cnt || targ.prompt(TWB.lang("unitsct"),0);
		if(cnt>0){
			var el=target.getElementById("TWB-UnitCnt-Spear");
			if(el){
				el.setAttribute("value",el.getAttribute("value")-(-cnt));
			}
			else{
				el=target.createElement("label");
				el.setAttribute("class","units");
				el.setAttribute("id","TWB-UnitCnt-Spear");
				el.setAttribute("value",cnt);
				el_=target.createElement("toolbarbutton");
				el_.setAttribute("id","TWB-Spear");
				el_.setAttribute("class","units");
				el_.addEventListener("click",TWB.Modules.Units.Delete,false);
				var ref=target.getElementById("TWB-Cancel");
				var par=ref.parentNode;
				par.insertBefore(el,ref);
				par.insertBefore(el_,ref);
			}
		}
	},
	Sword : function(targ,cnt){
		targ=targ || window;
		var target=(typeof targ=="undefined")?document:targ.document;
		cnt=cnt || targ.prompt(TWB.lang("unitsct"),0);
		if(cnt>0){
			var el=target.getElementById("TWB-UnitCnt-Sword");
			if(el){
				el.setAttribute("value",el.getAttribute("value")-(-cnt));
			}
			else{
				el=target.createElement("label");
				el.setAttribute("class","units");
				el.setAttribute("id","TWB-UnitCnt-Sword");
				el.setAttribute("value",cnt);
				el_=target.createElement("toolbarbutton");
				el_.setAttribute("class","units");
				el_.addEventListener("click",TWB.Modules.Units.Delete,false);
				el_.setAttribute("id","TWB-Sword");
				var ref=target.getElementById("TWB-Cancel");
				var par=ref.parentNode;
				par.insertBefore(el,ref);
				par.insertBefore(el_,ref);
			}
		}
	},
	Axe : function(targ,cnt){
		targ=targ || window;
		var target=(typeof targ=="undefined")?document:targ.document;
		cnt=cnt || targ.prompt(TWB.lang("unitsct"),0);
		if(cnt>0){
			var el=target.getElementById("TWB-UnitCnt-Axe");
			if(el){
				el.setAttribute("value",el.getAttribute("value")-(-cnt));
			}
			else{
				el=target.createElement("label");
				el.setAttribute("class","units");
				el.setAttribute("id","TWB-UnitCnt-Axe");
				el.setAttribute("value",cnt);
				el_=target.createElement("toolbarbutton");
				el_.setAttribute("class","units");
				el_.addEventListener("click",TWB.Modules.Units.Delete,false);
				el_.setAttribute("id","TWB-Axe");
				var ref=target.getElementById("TWB-Cancel");
				var par=ref.parentNode;
				par.insertBefore(el,ref);
				par.insertBefore(el_,ref);
			}
		}
	},
	Archer : function(targ,cnt){
		targ=targ || window;
		var target=(typeof targ=="undefined")?document:targ.document;
		cnt=cnt || targ.prompt(TWB.lang("unitsct"),0);
		if(cnt>0){
			var el=target.getElementById("TWB-UnitCnt-Archer");
			if(el){
				el.setAttribute("value",el.getAttribute("value")-(-cnt));
			}
			else{
				el=target.createElement("label");
				el.setAttribute("class","units");
				el.setAttribute("id","TWB-UnitCnt-Archer");
				el.setAttribute("value",cnt);
				el_=target.createElement("toolbarbutton");
				el_.setAttribute("class","units");
				el_.addEventListener("click",TWB.Modules.Units.Delete,false);
				el_.setAttribute("id","TWB-Archer");
				var ref=target.getElementById("TWB-Cancel");
				var par=ref.parentNode;
				par.insertBefore(el,ref);
				par.insertBefore(el_,ref);
			}
		}
	},
	Spy : function(targ,cnt){
		targ=targ || window;
		var target=(typeof targ=="undefined")?document:targ.document;
		cnt=cnt || targ.prompt(TWB.lang("unitsct"),0);
		if(cnt>0){
			var el=target.getElementById("TWB-UnitCnt-Spy");
			if(el){
				el.setAttribute("value",el.getAttribute("value")-(-cnt));
			}
			else{
				el=target.createElement("label");
				el.setAttribute("class","units");
				el.setAttribute("id","TWB-UnitCnt-Spy");
				el.setAttribute("value",cnt);
				el_=target.createElement("toolbarbutton");
				el_.setAttribute("class","units");
				el_.addEventListener("click",TWB.Modules.Units.Delete,false);
				el_.setAttribute("id","TWB-Spy");
				var ref=target.getElementById("TWB-Cancel");
				var par=ref.parentNode;
				par.insertBefore(el,ref);
				par.insertBefore(el_,ref);
			}
		}
	}, 
	Light : function(targ,cnt){
		targ=targ || window;
		var target=(typeof targ=="undefined")?document:targ.document;
		cnt=cnt || targ.prompt(TWB.lang("unitsct"),0);
		if(cnt>0){
			var el=target.getElementById("TWB-UnitCnt-Light");
			if(el){
				el.setAttribute("value",el.getAttribute("value")-(-cnt));
			}
			else{
				el=target.createElement("label");
				el.setAttribute("class","units");
				el.setAttribute("id","TWB-UnitCnt-Light");
				el.setAttribute("value",cnt);
				el_=target.createElement("toolbarbutton");
				el_.setAttribute("class","units");
				el_.addEventListener("click",TWB.Modules.Units.Delete,false);
				el_.setAttribute("id","TWB-Light");
				var ref=target.getElementById("TWB-Cancel");
				var par=ref.parentNode;
				par.insertBefore(el,ref);
				par.insertBefore(el_,ref);
			}
		}
	},
	Marcher : function(targ,cnt){
		targ=targ || window;
		var target=(typeof targ=="undefined")?document:targ.document;
		cnt=cnt || targ.prompt(TWB.lang("unitsct"),0);
		if(cnt>0){
			var el=target.getElementById("TWB-UnitCnt-Marcher");
			if(el){
				el.setAttribute("value",el.getAttribute("value")-(-cnt));
			}
			else{
				el=target.createElement("label");
				el.setAttribute("class","units");
				el.setAttribute("id","TWB-UnitCnt-Marcher");
				el.setAttribute("value",cnt);
				el_=target.createElement("toolbarbutton");
				el_.setAttribute("class","units");
				el_.addEventListener("click",TWB.Modules.Units.Delete,false);
				el_.setAttribute("id","TWB-Marcher");
				var ref=target.getElementById("TWB-Cancel");
				var par=ref.parentNode;
				par.insertBefore(el,ref);
				par.insertBefore(el_,ref);
			}
		}
	},
	Heavy : function(targ,cnt){
		targ=targ || window;
		var target=(typeof targ=="undefined")?document:targ.document;
		cnt=cnt || targ.prompt(TWB.lang("unitsct"),0);
		if(cnt>0){
			var el=target.getElementById("TWB-UnitCnt-Heavy");
			if(el){
				el.setAttribute("value",el.getAttribute("value")-(-cnt));
			}
			else{
				el=target.createElement("label");
				el.setAttribute("class","units");
				el.setAttribute("id","TWB-UnitCnt-Heavy");
				el.setAttribute("value",cnt);
				el_=target.createElement("toolbarbutton");
				el_.setAttribute("class","units");
				el_.addEventListener("click",TWB.Modules.Units.Delete,false);
				el_.setAttribute("id","TWB-Heavy");
				var ref=target.getElementById("TWB-Cancel");
				var par=ref.parentNode;
				par.insertBefore(el,ref);
				par.insertBefore(el_,ref);
			}
		}
	},
	Ram : function(targ,cnt){
		targ=targ || window;
		var target=(typeof targ=="undefined")?document:targ.document;
		cnt=cnt || targ.prompt(TWB.lang("unitsct"),0);
		if(cnt>0){
			var el=target.getElementById("TWB-UnitCnt-Ram");
			if(el){
				el.setAttribute("value",el.getAttribute("value")-(-cnt));
			}
			else{
				el=target.createElement("label");
				el.setAttribute("class","units");
				el.setAttribute("id","TWB-UnitCnt-Ram");
				el.setAttribute("value",cnt);
				el_=target.createElement("toolbarbutton");
				el_.setAttribute("class","units");
				el_.addEventListener("click",TWB.Modules.Units.Delete,false);
				el_.setAttribute("id","TWB-Ram");
				var ref=target.getElementById("TWB-Cancel");
				var par=ref.parentNode;
				par.insertBefore(el,ref);
				par.insertBefore(el_,ref);
			}
		}
	},
	Catapult : function(targ,cnt){
		targ=targ || window;
		var target=(typeof targ=="undefined")?document:targ.document;
		cnt=cnt || targ.prompt(TWB.lang("unitsct"),0);
		if(cnt>0){
			var el=target.getElementById("TWB-UnitCnt-Catapult");
			if(el){
				el.setAttribute("value",el.getAttribute("value")-(-cnt));
			}
			else{
				el=target.createElement("label");
				el.setAttribute("class","units");
				el.setAttribute("id","TWB-UnitCnt-Catapult");
				el.setAttribute("value",cnt);
				el_=target.createElement("toolbarbutton");
				el_.setAttribute("class","units");
				el_.addEventListener("click",TWB.Modules.Units.Delete,false);
				el_.setAttribute("id","TWB-Catapult");
				var ref=target.getElementById("TWB-Cancel");
				var par=ref.parentNode;
				par.insertBefore(el,ref);
				par.insertBefore(el_,ref);
			}
		}
	},
	Knight : function(targ,cnt){
		targ=targ || window;
		var target=(typeof targ=="undefined")?document:targ.document;
		cnt=cnt || targ.prompt(TWB.lang("unitsct"),0);
		if(cnt>0){
			var el=target.getElementById("TWB-UnitCnt-Knight");
			if(el){
				el.setAttribute("value",el.getAttribute("value")-(-cnt));
			}
			else{
				el=target.createElement("label");
				el.setAttribute("class","units");
				el.setAttribute("id","TWB-UnitCnt-Knight");
				el.setAttribute("value",cnt);
				el_=target.createElement("toolbarbutton");
				el_.setAttribute("class","units");
				el_.addEventListener("click",TWB.Modules.Units.Delete,false);
				el_.setAttribute("id","TWB-Knight");
				var ref=target.getElementById("TWB-Cancel");
				var par=ref.parentNode;
				par.insertBefore(el,ref);
				par.insertBefore(el_,ref);
			}
		}
	},
	Snob : function(targ,cnt){
		targ=targ || window;
		var target=(typeof targ=="undefined")?document:targ.document;
		cnt=cnt || targ.prompt(TWB.lang("unitsct"),0);
		if(cnt>0){
			var el=target.getElementById("TWB-UnitCnt-Snob");
			if(el){
				el.setAttribute("value",el.getAttribute("value")-(-cnt));
			}
			else{
				el=target.createElement("label");
				el.setAttribute("class","units");
				el.setAttribute("id","TWB-UnitCnt-Snob");
				el.setAttribute("value",cnt);
				el_=target.createElement("toolbarbutton");
				el_.setAttribute("class","units");
				el_.addEventListener("click",TWB.Modules.Units.Delete,false);
				el_.setAttribute("id","TWB-Snob");
				var ref=target.getElementById("TWB-Cancel");
				var par=ref.parentNode;
				par.insertBefore(el,ref);
				par.insertBefore(el_,ref);
			}
		}
	},
	Cancel : function(targ){
		targ=targ || window;
		var target=(typeof targ=="undefined")?document:targ.document;
		var i;
		var uns=target.getElementsByClassName("units");
		for(i=uns.length-1; i>=0; i--){
			uns[i].parentNode.removeChild(uns[i]);
		}
	},
	Submit : function(targ,callback){
		var i;
		var unit;
		var cnt;
		var units="";
		targ=targ || window;
		var target=(typeof targ=="undefined")?document:targ.document;
		var uns=target.getElementsByClassName("units");
		for(i=0; i<uns.length; i++){
			if(uns[i].nodeName!="label"){
				continue;
			}
			cnt=uns[i].getAttribute("value");
			unit=uns[i].getAttribute("id").replace("TWB-UnitCnt-","").toLowerCase();
			switch(unit){
				case "spear": unit="p"; break;
				case "sword": unit="w"; break;
				case "axe": unit="x"; break;
				case "archer": unit="e"; break;
				case "spy": unit="s"; break;
				case "light": unit="l"; break;
				case "marcher": unit="m"; break;
				case "heavy": unit="h"; break;
				case "ram": unit="r"; break;
				case "catapult": unit="c"; break;
				case "knight": unit="k"; break;
				case "snob": unit="b"; break;
			}
			units+=(cnt+unit);
		}
		// Callback
		if(typeof callback=="undefined"){
			var func=TWB.Modules.Units.Action.split(".");
			var tmp=TWB;
			var i;
			for(i=0; i<func.length; i++){
				tmp=tmp[func[i]];
			}
			TWB.Modules.Units.Action=null;
			TWB.Modules.Units.Cancel();
			TWB.Modules.Units.Hide();
			tmp(units);
		}
		else{
			TWB.Modules.Units.Cancel(targ);
			TWB.Modules.Units.Hide(targ);
			callback(units);
		}
	},
	String2Obj : function(str){
		var i;
		var obj={};
		str=str.match(/\d+[a-z]/g);
		for(i=0; i<str.length; i++){
			var unit=null;
			switch(str[i].match(/[a-z]/)[0]){
				case "p": unit="spear"; break;
				case "w": unit="sword"; break;
				case "x": unit="axe"; break;
				case "e": unit="archer"; break;
				case "s": unit="spy"; break;
				case "l": unit="light"; break;
				case "m": unit="marcher"; break;
				case "h": unit="heavy"; break;
				case "r": unit="ram"; break;
				case "c": unit="catapult"; break;
				case "k": unit="knight"; break;
				case "b": unit="snob"; break;
			}
			if(unit!=null){
				obj[unit]=str[i].match(/\d+/)[0];
			}
		}
		return obj;
	},
});
